﻿@model ArticleModel
@using CAF.WebSite.Application.WebUI
@using CAF.WebSite.Application.WebUI.UI;
@using CAF.Infrastructure.Core;

@if (Model.ShowAttributes)
{
    <script>
    var stock_val = '@Html.Raw(Model.ProductSpecifications)';
    stock_val = JSON.parse(stock_val);
    $(function () {
        //清空表单数据

        if (stock_val) {

            drawThead()//绘制表头
            drawTbody()//绘制表格
            chandate();
            $("[data-toggle=popover]").popover();

        }
        else { $("#ProductSpecifications").val(""); }

        //初始化
        cout()


        //添加规格
        $("#suk_addsuk").click(function () {
            $.ajax({
                url: "/seller/article/allproductvariantattributes",
                data: { label: '', selectedId: 0, },
                success: function (res) {
                    var size = $(".suk_body>section").size() + 1;
                    //第一个规格添加图片
                    var ispic = "";
                    var ispicwrap = '';
                    if (size == 1) {
                        ispic = '<div class="checkbox i-checks checkbox-inline" style="margin-left: 25px"><label><input type="checkbox" id="js-addImg-function" class="icheck" value="1"><i></i>添加规格图片</label></div> ';
                        ispicwrap = '<div id="upload-img-wrap"></div>';
                    }


                    var suk_select = ""
                    suk_select += '<section class="panel panel-default m-b-none" data-id="select-' + size + '">' +
                                    '<header class="panel-heading bg-light no-border">' +
                                    '<div class="clearfix">' +
                                    '<div class="row">' +
                                    '<div class="col-md-5">' +
                                    '<select style="width:260px" class="chosen-select  form-control" data-sid="' + size + '" data-placeholder="请选择规格">' +
                                    '<option value=""></option>';
                                    $.each(res, function (index, item) {
                                        suk_select += '<option value="' + item.id + '">' + item.text + '</option>'
                                    })

                                    suk_select += '</select>' +
                                    '</div>' +
                                    '<div class="col-md-5">' +
                                    ispic +
                                    '</div>' +
                                     '<div class="col-md-2">' +
                                    '<a class="close suk_close" id="select-' + size + '" >×</a>' +
                                     '</div>' +
                                    '</div>' +
                                    '</div>' +
                                    '</header>' +
                                    '<div class="list-group no-radius alt">' +
                                    '<div href="#" class="list-group-item">' +
                                    ' <ul class="sys_spec_text" id="sys_spec-' + size + '" data-sid="' + size + '"></ul>' +
                                    '</div>' +
                                    '</div>' +
                                    ispicwrap +
                                    '</section>';

                    $('.suk_body').append(suk_select);

                    cout();

                }
            })
        })

        $("#productSpecId").on('click', '.sys_spec_text li', function () {
            var size = $(this).data("sid");
            if (!!$(this).hasClass("selected")) {
                $(this).removeClass("selected");

            } else {
                $(this).addClass("selected");
            }
            if (size == 1 && $("#js-addImg-function").attr("checked")) {
                drawPicBox();
            }

            drawThead()//绘制表头
            drawTbody()//绘制表格
            //当有规格添加时锁定价格和库存
            $("#present_price").attr("disabled", true);
            $("input[name='weight']").attr("disabled", true);
        })
        $("#productSpecId").on('change', 'select.chosen-select', function (e) {
            var selectedId = $(this).val();
            var size = $(this).data("sid");
            var $eventSelect = $('.suk_body').find('#sys_spec-' + size);
            $eventSelect.html("");
            $.ajax({
                url: "/seller/article/productattributevaluelist",
                data: { productAttributeId: selectedId, },
                success: function (opres) {
                    var opdata = [];
                    var statesOptions = "";

                    $.each(opres, function (index, option) {
                        statesOptions += '<li style="background:' + option.color + '" data-color="' + option.color + '" data-aid="' + option.id + '"><a href="javascript:;" title="' + option.text + '">' + option.text + '</a><i></i></li>'

                    })
                    $eventSelect.append(statesOptions);

                },
            });
            selectDisabled();
        });





        //批量设置价格
        $("#productSpecId").on('click', "#js-batch-price", function () {
            var price = $("#price-batch-txt").val();
            $(".sku_price").val(price);
            chandate();
            $('.popover-hide').popover('hide');
        });
        $("#productSpecId").on('click', "#js-batch-stock", function () {
            var stock = $("#stock-batch-txt").val();
            $(".sku_stock").val(stock);
            chandate();
            $('.popover-hide').popover('hide');
        });
        $("#productSpecId").on('click', "#js-batch-weight", function () {
            var weight = $("#weight-batch-txt").val();
            $(".sku_weight").val(weight);
            chandate();
            $('.popover-hide').popover('hide');
        });
        $("#productSpecId").on('click', "#js-batch-sku", function () {
            var skucode = $("#sku-batch-txt").val();
            $(".sku_code").val(skucode);
            chandate();
            $('.popover-hide').popover('hide');
        });

        $("#productSpecId").on('click', '.popover-title .close', function (e) {
            var $target = $(e.target), $popover = $target.closest('.popover').prev();
            $popover && $popover.popover('hide');
        });
        $("#productSpecId").on('change', 'select.chosen-select', function () {
            var val = $('select.chosen-select').val();//获取选中的值
            var tex = $('select.chosen-select option:selected').text();//选择选择的文字
            drawThead()//绘制表头
            //console.log(val);
            //console.log(tex);
        })

        //删除图片
        $("#productSpecId").on('click', ".js-remove-sku-atom", function () {
            //找到父节点清除
            var par = $(this).parents(".uploader-list");
            par.html("");
            //图片从服务器上删除 todo

            //显示上传按钮
            par.next("div.webuploader-container").removeClass("hide");
            //清空图片
            par.prev("input").val("")
            //删除图片后改变数据机构
            chandate();

        })

        //是否添加图片
        $("#productSpecId").on("click", "#js-addImg-function", function () {

            if ($(this).prop("checked")) {

                drawPicBox();
                chandate();
            } else {
                $("#upload-img-wrap").html("");
                chandate();
            }
        })

        // 储存数据
        $("#productSpecId").on('change', '.sku_price,.sku_stock,.sku_code,.sku_weight', function () {
            chandate();
        })
        //删除规格
        $("#productSpecId").on('click', ".suk_close", function () {
            var select = $(this).attr("id");

            $('[data-id="' + select + '"]').remove();
            $.each($("section.suk_body>section"), function (i, v) {
                i = i + 1;

                $(v).attr("data-id", "select-" + i);
                $(v).find(".suk_close").attr("id", "select-" + i);
            })
            drawThead()//绘制表头
            drawTbody()//绘制表格
            cout();
        })
        $("#productSpecId").on('click', '.js-tags', function () {
            var section = $(this).parents('section').attr('data-id');
            section = '[data-id=' + section + ']';
            var input = $(section).find("input.js-tas-input").val();
            if (input.length == 0) {
                toastr.error("请填写规格！");
                return false;
            }
            $.ajax({
                type: "POST",
                url: "/seller/tags/ajaxaddtags",
                data: { name: input, type: 1, model_id: "4" },
                success: function (res) {
                    if (res.errno == 0) {
                        $(section).find("select.chosen-select").append("<option value=" + res.id + " selected>" + res.name + "</option>");
                        $('select.chosen-select').trigger("liszt:updated");
                        $('select.popover-hide').popover('hide');
                        toastr.success("添加规格成功！");
                    } else {
                        toastr.error(res.errmsg);
                    }
                }

            })
        })

        //关联价格
        $("#productSpecId").on('change', '#present_price,#discount_price', function () {
            var discount_price;
            if ($("#discount_price").val()) {
                discount_price = $("#discount_price").val()
            } else {
                discount_price = 0
            }
            $("#price").val('{"present_price":"' + $("#present_price").val() + '","discount_price":"' + discount_price + '"}')
        })

    })
    //绘制第一个规格下面每个标签的图片
    function drawPicBox() {
        var section = $('section.suk_body>section').eq(0);
        var tags = section.find(".sys_spec_text li.selected")
        var size = tags.size();
        var odlediv = $("#upload-img-wrap>div")
        //获取上次的结构
        var arr = []
        $.each(odlediv, function (k, v) {
            var obj = {};
            // var html = '<div class="uplodbox" id="box_'+k+'">'+$(v).html()+'</div>'
            //console.log(html);
            var val = $(v).find("input").val();
            var calss = $(v).find(".webuploader-container").attr("class");
            var html = $(v).find(".uploader-list").html()
            obj.val = val;
            obj.class = calss;
            obj.html = html;
            arr.push(obj);
        })
        console.log(arr)
        $.each(tags, function (k, v) {
            var tagsname = $(v).text()
            var pic_upload;

            if (arr[k]) {
                pic_upload = '<div class="uplodbox" id="box_' + k + '"><input type="hidden" id="cover_id_' + k + '" value="' + arr[k]['val'] + '"/>\
                                 <div id="fileList_'+ k + '" class="uploader-list">' + arr[k]['html'] + '</div>\
                                 <div id="filePicker_'+ k + '" data-id="' + k + '" class="' + arr[k]['class'] + '"> <i class="fa fa-file-image-o" style="font-size: 36px;margin-top: 15px"></i></br>' + tagsname + '</div></div>'
                console.log(pic_upload);
            } else {
                pic_upload = '<div class="uplodbox" id="box_' + k + '"><input type="hidden" id="cover_id_' + k + '"/>\
                                 <div id="fileList_'+ k + '" class="uploader-list"></div>\
                                 <div id="filePicker_'+ k + '" data-id="' + k + '"> <i class="fa fa-file-image-o" style="font-size: 36px;margin-top: 15px"></i></br>' + tagsname + '</div></div>'
            }



            var imgwrap = section.find("#upload-img-wrap")
            if (k == 0) {
                imgwrap.html(pic_upload);
            } else {
                imgwrap.append(pic_upload);
            }

            uploadpic(k, tagsname);

        })


    }
    //上传图片
    function uploadpic(size, name) {
        $list = $('#fileList_' + size),
        //console.log($list);
        // 优化retina, 在retina下这个值是2
           ratio = window.devicePixelRatio || 1,

        // 缩略图大小
           thumbnailWidth = 100 * ratio,
           thumbnailHeight = 100 * ratio,

        // Web Uploader实例
            uploader;

        // 初始化Web Uploader
        // 初始化Web Uploader
        var uploader = WebUploader.create({
            // 选完文件后，是否自动上传。
            auto: true,
            // swf文件路径
            swf: '/Content/assets/global/plugins/webuploader/Uploader.swf',
            // 文件接收服务端。
            server: '/seller/Picture/AsyncUpload',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                id: '#filePicker_' + size,
                multiple: false
            },
            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            }
        });
        // 当有文件添加进来的时候
        uploader.on('fileQueued', function (file) {
            var $li = $(
                            '<div id="' + file.id + '" class="file-item thumbnail">' +
                            '<img>' +
                            '<div class="info">' + name + '</div>' +
                            '<div class="atom-close close-modal small js-remove-sku-atom">×</div>' +
                            '</div>'
                    ),
                    $img = $li.find('img');

            $('#fileList_' + size).html($li);
            $('#filePicker_' + size).addClass("hide");

            //记录dome
            // 创建缩略图
            uploader.makeThumb(file, function (error, src) {
                if (error) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }

                $img.attr('src', src);
            }, thumbnailWidth, thumbnailHeight);
        });

        // 文件上传过程中创建进度条实时显示。
        uploader.on('uploadProgress', function (file, percentage) {

            var $li = $('#' + file.id),
                    $percent = $li.find('.progress span');

            // 避免重复创建
            if (!$percent.length) {
                $percent = $('<p class="progress"><span></span></p>')
                        .appendTo($li)
                        .find('span');
            }

            $percent.css('width', percentage * 100 + '%');
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on('uploadSuccess', function (file, res) {
            $('#' + file.id).addClass('upload-state-done');
            $("#cover_id_" + size).val(res.pictureId);
            //图片上传成功后改变数据
            chandate();
        });

        // 文件上传失败，现实上传出错。
        uploader.on('uploadError', function (file) {
            var $li = $('#' + file.id),
                    $error = $li.find('div.error');

            // 避免重复创建
            if (!$error.length) {
                $error = $('<div class="error"></div>').appendTo($li);
            }

            $error.text('上传失败');
        });

        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on('uploadComplete', function (file) {
            $('#' + file.id).find('.progress').remove();
        });
    }

    function chandate() {
        var type = gettype();
        var narr = getstru();
        localStorage.setItem('$narr', JSON.stringify(narr));

        var ntr = $(".suk_table").find("tbody>tr");
        var bianli_arr = [];
        $.each(ntr, function (k, v) {
            var _obj = {};
            _obj.sku_price = $("#sku_price_" + k).val();
            _obj.sku_stock = $("#sku_stock_" + k).val();
            _obj.sku_weight = $("#sku_weight_" + k).val();
            _obj.sku_code = $("#sku_code_" + k).val();
            bianli_arr.push(_obj);
        });
        //console.log(JSON.stringify(bianli_arr));

        function bianli(arr) {
            for (var i = 0; i < arr.length; i++) {
                try {
                    if (arr[i].ch) {
                        bianli(arr[i].ch);
                    } else {
                        var sh = bianli_arr.shift();
                        //console.log(sh);
                        //console.log(arr[i]);
                        for (var k in sh) {
                            arr[i][k] = sh[k];
                        }
                    }
                } catch (e) {
                }
            }
            return arr;
        }
        //console.log(JSON.stringify(narr));
        var result = bianli(JSON.parse(localStorage.getItem("$narr")));
        // console.log(JSON.stringify(result));

        //拼装储存数据
        var suk_info = {
            "type": type,
            "data": result,
            "is_pic": $("#js-addImg-function:checked").val() ? $("#js-addImg-function:checked").val() : 0
        }

        if (type.length > 0) {
            $("#ProductSpecifications").val(JSON.stringify(suk_info));
        } else {
            $("#ProductSpecifications").val("");
        }
        //关联价格
        var pricearr = [];
        $.each($(".sku_price"), function () {
            pricearr.push($(this).val());
        })
        pricearr.sort(function (a, b) {
            return a - b;
        });
        //折扣价
        var discount_price = $("#discount_price").val();
        if (discount_price == "") {
            discount_price = 0;
        }
        //设置价格
        if (pricearr[pricearr.length - 1] == pricearr[0]) {
            $("@Html.FieldIdFor(model => model.Price)").val(pricearr[0]);
            //$("#present_price").val(pricearr[0]);
            //var p = '{"present_price":"' + pricearr[0] + '","discount_price":"' + discount_price + '"}'
            //$("#price").val(p);
        } else {
            $("@Html.FieldIdFor(model => model.Price)").val(pricearr[0] + "-" + pricearr[pricearr.length - 1]);
            //$("#present_price").val(pricearr[0] + "-" + pricearr[pricearr.length - 1]);
            //var p = '{"present_price":"' + pricearr[0] + '-' + pricearr[pricearr.length - 1] + '","discount_price":"' + discount_price + '"}'
            //$("#price").val(p);
        }
        //关联库存
        var total_stockarr = 0;
        $.each($(".sku_stock"), function () {
            total_stockarr = Number($(this).val()) + total_stockarr;
        })
        // $("input[name='total_stock']").val(total_stockarr);
        $("@Html.FieldIdFor(model => model.StockQuantity)").val(total_stockarr);
    }


    //绘制表头
    function drawThead() {
        var arr = $('select.chosen-select');
        var narr = [];
        $.each(arr, function (k, v) {
            if ($(v).find("option:selected").text().length > 0)
                narr.push($(v).find("option:selected").text());
        })
        //console.log(narr);
        var thHtml = "";
        thHtml += "";
        thHtml += "<tr>";
        for (cSort = 0; cSort <= narr.length - 1; cSort++) {
            thHtml += '<th class="">' + narr[cSort] + "</th>";
        }
        thHtml += '<th class="">价格</th><th class="">库存</th><th class="">重量（件/g）</th><th class="">编码</th>';
        thHtml += "</tr>";
        thHtml += "";
        $(".suk_table").removeClass("hide");
        $(".suk_table table thead").html(thHtml);
    }


    //绘制表格
    function drawTbody() {
        $(".suk_table").removeClass("hide");
        var type = gettype();
        var narr = getstru();
        var i = 0;
        var tbHtml = "";
        var tbHtml1 = "";
        var tbHtml2 = "";
        var v_sku_price = "";
        var v_sku_stock = "";
        var v_sku_weight = "";
        var v_sku_code = "";
        //获取上次chucun的数据；
        var predatas = $("#ProductSpecifications").val();

        if (predatas) {
            var predata = $.parseJSON(predatas).data;
        }
        //console.log(predata);

        $.each(narr, function (k, v) {
            if (v.ch) {
                $.each(v.ch, function (_k, _v) {
                    if (_v.ch) {

                        $.each(_v.ch, function (__k, __v) {
                            try {
                                var v_sku_price = "";
                                var v_sku_stock = "";
                                var v_sku_code = "";
                                var d_ = predata[k]['ch'][_k]["ch"][__k];
                                v_sku_price = d_['sku_price'] ? d_['sku_price'] : "0";
                                v_sku_stock = d_['sku_stock'] ? d_['sku_stock'] : "0";
                                v_sku_code = d_['sku_code'] ? d_['sku_code'] : "";
                                v_sku_weight = d_['sku_weight'] ? d_['sku_weight'] : "0";
                            } catch (error) {
                                // 在错误发生时怎么处理
                            }
                            var g = v.ch.length * _v.ch.length;
                            tbHtml += "<tr>";
                            if (i % g == 0) {
                                tbHtml += "<td rowspan=" + v.ch.length * _v.ch.length + ">" + v.name + "</td>";
                            }
                            if (__k % _v.ch.length == 0) {
                                tbHtml += "<td rowspan=" + _v.ch.length + ">" + _v.name + "</td>";
                            }
                            tbHtml += "<td rowspan='1'>" + __v.name + "</td>";
                            tbHtml += '<td><input type="text" placeholder="价格" value="' + v_sku_price + '" class="form-control input-sm sku_price"  id="sku_price_' + i + '"></td>\
                        <td><input type="text" placeholder="库存" value="'+ v_sku_stock + '" class="form-control input-sm sku_stock" id="sku_stock_' + i + '"></td>\
                        <td><input type="text" placeholder="重量" value="'+ v_sku_weight + '" class="form-control input-sm sku_weight" id="sku_weight_' + i + '"></td>\
                        <td><input type="text" placeholder="编码" value="'+ v_sku_code + '" class="form-control input-sm sku_code" id="sku_code_' + i + '"></td>\
                        ';
                            tbHtml += "</tr>";
                            var sku_price = "#sku_price_" + i;
                            var sku_stock = "#sku_stock_" + i;
                            var sku_code = "#sku_code_" + i;
                            var sku_weight = "#sku_weight_" + i;
                            __v.sku_weight = $(sku_weight).val();
                            __v.sku_price = $(sku_price).val();
                            __v.sku_stock = $(sku_stock).val();
                            __v.sku_code = $(sku_code).val();
                            i = i + 1;
                        })
                    } else {

                        try {
                            // 可能会导致错误的代码
                            var d_ = predata[k]["ch"][_k];
                            v_sku_price = d_['sku_price'] ? d_['sku_price'] : "0";
                            v_sku_stock = d_['sku_stock'] ? d_['sku_stock'] : "0";
                            v_sku_weight = d_['sku_weight'] ? d_['sku_weight'] : "0";
                            v_sku_code = d_['sku_code'] ? d_['sku_code'] : "";
                        } catch (error) {
                            // 在错误发生时怎么处理
                        }
                        tbHtml += "<tr>";
                        if (_k % v.ch.length == 0) {
                            tbHtml += "<td rowspan=" + v.ch.length + ">" + v.name + "</td>";
                        }
                        tbHtml += "<td rowspan='1'>" + _v.name + "</td>";
                        tbHtml += '<td><input type="text" placeholder="价格" value="' + v_sku_price + '" class="form-control input-sm sku_price"  id="sku_price_' + i + '"></td>\
                        <td><input type="text" placeholder="库存" value="'+ v_sku_stock + '" class="form-control input-sm sku_stock" id="sku_stock_' + i + '"></td>\
                        <td><input type="text" placeholder="重量" value="'+ v_sku_weight + '" class="form-control input-sm sku_weight" id="sku_weight_' + i + '"></td>\
                        <td><input type="text" placeholder="编码" value="'+ v_sku_code + '" class="form-control input-sm sku_code" id="sku_code_' + i + '"></td>\
                        ';
                        tbHtml += "</tr>";
                        var sku_price = "#sku_price_" + i;
                        var sku_stock = "#sku_stock_" + i;
                        var sku_weight = "#sku_weight_" + i;
                        var sku_code = "#sku_code_" + i;
                        _v.sku_price = $(sku_price).val();
                        _v.sku_stock = $(sku_stock).val();
                        _v.sku_weight = $(sku_weight).val();
                        _v.sku_code = $(sku_code).val();
                        i = i + 1;
                    }
                })
            } else {
                //输出一级表格

                if (predata) {
                    if (predata[k]) {
                        //console.log(predata[k]['sku_price'] != null)
                        v_sku_price = predata[k]['sku_price'] ? predata[k]['sku_price'] : "0";
                        v_sku_stock = predata[k]['sku_stock'] ? predata[k]['sku_stock'] : "0";
                        v_sku_weight = predata[k]['sku_weight'] ? predata[k]['sku_weight'] : "0";
                        v_sku_code = predata[k]['sku_code'] ? predata[k]['sku_code'] : "";
                    }
                }
                tbHtml += "<tr>";
                tbHtml += "<td rowspan='1'>" + v.name + "</td>";
                tbHtml += '<td><input type="text" placeholder="价格" value="' + v_sku_price + '" class="form-control input-sm sku_price"  id="sku_price_' + i + '"></td>' +
                          '<td><input type="text" placeholder="库存" value="' + v_sku_stock + '" class="form-control input-sm sku_stock" id="sku_stock_' + i + '"></td>' +
                          '<td><input type="text" placeholder="重量" value="' + v_sku_weight + '" class="form-control input-sm sku_weight" id="sku_weight_' + i + '"></td>' +
                          '<td><input type="text" placeholder="编码" value="' + v_sku_code + '" class="form-control input-sm sku_code" id="sku_code_' + i + '"></td>';
                tbHtml += "</tr>";
                var sku_price = "#sku_price_" + i;
                var sku_stock = "#sku_stock_" + i;
                var sku_weight = "#sku_weight_" + i;
                var sku_code = "#sku_code_" + i;

                v.sku_price = $(sku_price).val();
                v.sku_stock = $(sku_stock).val();
                v.sku_weight = $(sku_weight).val();
                v.sku_code = $(sku_code).val();
                i = i + 1;
            }
        })
        //console.log(tbHtml1);

        $(".suk_table table tbody").html(tbHtml);
        //拼装储存数据
        var suk_info = {
            "type": type,
            "data": narr,
            "is_pic": $("#js-addImg-function:checked").val() ? $("#js-addImg-function:checked").val() : 0
        }
        if (type.length > 0) {
            $("#ProductSpecifications").val(JSON.stringify(suk_info));
        } else {
            $("#ProductSpecifications").val("");
        }

    }
    //记录添加规格的个数
    function cout() {
        var size = $(".suk_body>section").size();
        if (size != 0) {
            $(".suk_heading").removeClass("hide");
            $(".suk_body").removeClass("hide");
            $(".suk_table").removeClass("hide");


        } else {
            $(".suk_heading").addClass("hide");
            $(".suk_body").addClass("hide");
            $(".suk_table").addClass("hide");
            $("#present_price").attr("disabled", false);
            $("input[name='weight']").attr("disabled", false);
        }
        if (size > 2) {
            $("#suk_addsuk").addClass("hide");
        } else {
            $("#suk_addsuk").removeClass("hide");
        }
    }
    //获取类型
    function gettype() {
        var arr = $('select.chosen-select');
        var type = [];
        $.each(arr, function (k, v) {
            if ($(v).find("option:selected").text().length > 0)
                //type.push($(v).find("option:selected").text());
                type.push({ name: $(v).find("option:selected").text(), attrId: $(v).find("option:selected").val() });
        })
        return type;
    }
    //获取数据ID
    function gettypeId() {
        var arr = $('select.chosen-select');
        var type = [];
        $.each(arr, function (k, v) {
            if ($(v).find("option:selected").text().length > 0)
                //type.push($(v).find("option:selected").text());
                type.push($(v).find("option:selected").val());
        })
        return type;
    }
    //数据机构
    function getstru() {
        var narr = [];

        $.each($(".sys_spec_text"), function (k, v) {
            var type = gettype();
            //narr.push($(v).val());
            item = $(v).find("li.selected");
            //获取类型

            if (item.length != 0) {
                if (k == 0) {
                    $.each(item, function (n, m) {
                        var obj = {};
                        obj.name = $(m).find("a").html();
                        obj.type = type[k].name;
                        obj.typeid = $(m).data("aid");
                        obj.color = $(m).data("color");
                        // console.log(obj);
                        obj.pic = $("#cover_id_" + n).val();
                        narr.push(obj);
                    })

                }
                if (k == 1) {
                    var ch_arr = [];
                    $.each(item, function (nn, mm) {
                        //console.log(mm)
                        var _obj = {};
                        _obj.name = $(mm).find("a").html();
                        _obj.type = type[k].name;
                        _obj.typeid = $(mm).data("aid");
                        _obj.color = $(mm).data("color");
                        ch_arr.push(_obj);
                    });
                    for (var i = 0; i < narr.length; i++) {
                        narr[i].ch = ch_arr;
                    }
                }
                if (k == 2) {
                    var ch_arr = [];
                    $.each(item, function (nn, mm) {
                        //console.log(mm)
                        var _obj = {};
                        _obj.name = $(mm).find("a").html();
                        _obj.type = type[k].name;
                        _obj.typeid = $(mm).data("aid");
                        _obj.color = $(mm).data("color");
                        ch_arr.push(_obj);
                    });
                    for (var i = 0; i < narr.length; i++) {
                        for (var j = 0; j < narr[i].ch.length; j++) {
                            var tmp = narr[i].ch;
                            tmp[j].ch = ch_arr;
                        }
                    }
                }

            }

        })
        return narr;
    }
    //禁用已经选择的选项
    function selectDisabled() {
        var arr = $('select.chosen-select');
        var narr = [];
        var typeIds = gettypeId();
        $.each(arr, function (k, v) {
            var options = $(v).find("option");
            $.each(options, function (opk, opv) {
                if ($.inArray($(opv).val(), typeIds) > -1) {
                    $(opv).attr("disabled", "disabled");
                }
                else {
                    $(opv).removeAttr("disabled");
                }
            })
        })
    }
    </script>
    <div class="form-group">
        <label class="control-label col-md-2">
            <span class="help-block m-b-none text-muted"><i class="fa fa-info-circle text-info"></i> 库存/规格</span>
        </label>
        <div class="col-md-10">
            <div class="col-md-10" id="productSpecId">
                <input type="hidden" name="ProductSpecifications" class="form-control" value="@Model.ProductSpecifications" id="ProductSpecifications" />

                <section class="panel panel-default">
                    <header class="panel-heading suk_heading hide">
                        商品规格
                    </header>
                    <section class="chat-list panel-body suk_body hide">
                        @Html.Partial("_ProductAttributes", Model.ProductVariantAttributeCombinationSku)
                    </section>
                    <footer class="panel-footer btn btn-block" id="suk_addsuk">
                        <!-- chat form -->
                        <i class="fa fa-plus text"></i> 添加规格项目
                    </footer>
                </section>
                <section class="portlet box panel panel-default suk_table hide">
                    <header class="panel-heading">
                        商品库存
                    </header>
                    <div class="portlet-body">

                        <div class="table-scrollable">
                            <table class="table table-bordered table-hover b-t b-light">
                                <thead> </thead>
                                <tbody></tbody>
                            </table>
                        </div>
                    </div>
                    <footer class="panel-footer">
                        <div class="row">
                            <div class="col-sm-10 hidden-xs">
                                批量设置:
                                <a class="btn btn-sm btn-default popover-hide" title="" data-html="true"
                                   data-toggle="popover" data-placement="bottom" data-original-title='<button type="button" class="close pull-right" data-dismiss="popover">&times;</button>设置价格'
                                   data-content="<div class='form-inline'><div class='form-group '><input type='text' placeholder='请输入价格' id='price-batch-txt' class='form-control input-sm m-l'></div><a class='btn btn-success btn-sm ' id='js-batch-price' href='javascript:;'>保存</a></div>">价格</a>
                                <a class="btn btn-sm btn-default popover-hide" title="" data-html="true"
                                   data-toggle="popover" data-placement="bottom" data-original-title='<button type="button" class="close pull-right" data-dismiss="popover">&times;</button>设置库存'
                                   data-content="<div class='form-inline'><div class='form-group '><input type='text' placeholder='请输入库存' id='stock-batch-txt' class='form-control input-sm m-l'></div><a class='btn btn-success btn-sm ' id='js-batch-stock'  href='javascript:;'>保存</a></div>">库存</a>
                                <a class="btn btn-sm btn-default popover-hide" title="" data-html="true"
                                   data-toggle="popover" data-placement="bottom" data-original-title='<button type="button" class="close pull-right" data-dismiss="popover">&times;</button>设置重量（件/g）'
                                   data-content="<div class='form-inline'><div class='form-group '><input type='text' placeholder='请输入单价商品的重量' id='weight-batch-txt' class='form-control input-sm m-l'></div><a class='btn btn-success btn-sm ' id='js-batch-weight'  href='javascript:;'>保存</a></div>">重量（件/g）</a>
                                <a class="btn btn-sm btn-default popover-hide" title="" data-html="true"
                                   data-toggle="popover" data-placement="bottom" data-original-title='<button type="button" class="close pull-right" data-dismiss="popover">&times;</button>设置Sku编码'
                                   data-content="<div class='form-inline'><div class='form-group '><input type='text' placeholder='请输入商品的Sku编码' id='sku-batch-txt' class='form-control input-sm m-l'></div><a class='btn btn-success btn-sm ' id='js-batch-sku'  href='javascript:;'>保存</a></div>">Sku编码）</a>
                            </div>

                        </div>
                    </footer>
                </section>

            </div>
        </div>
    </div>
}




    @* SKU
        "{"type":["颜色","尺寸","款式"],"data":
        [
        {"name":"蓝色","type":"颜色","ch":

        [{"name":"38","type":"尺寸","ch":
        [{"name":"冬季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""},
        {"name":"夏季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""}]}
        ,{"name":"39","type":"尺寸","ch":
        [{"name":"冬季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""},
        {"name":"夏季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""}]}
        ]

        },
        {"name":"绿色","type":"颜色","ch":

        [{"name":"38","type":"尺寸","ch":
        [{"name":"冬季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""},
        {"name":"夏季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""}]},
        {"name":"39","type":"尺寸","ch":
        [{"name":"冬季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""},
        {"name":"夏季","type":"款式","sku_price":"100","sku_stock":"200","sku_weight":"10","sku_code":""}]}
        ]

        }
        ],"is_pic":0}"
    *@